# -*- coding: utf-8 -*-
# File : common_utils.py
# Author: taoyahui
# Date : 2021/10/17
import random
import datetime
import time
import hashlib
import requests
import json
import config

def get_token():
    """
    随机生成token
    """
    src = ''.join(random.sample('abcdefghijklmnopqrstuvwxyz!@#$%^&*()', 20))
    m2 = hashlib.md5()
    m2.update(src.encode("utf8"))
    return m2.hexdigest()


def get_time(num):
    """
    将区块链时间转为时间格式
    """
    timeArray = time.localtime(int(num/1000))
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    return otherStyleTime


def common_bc_req(user_address, contract_name, func_name, param, contract_address, abi):
    """
    通用调用区块链智能合约方法
    """
    data = {
        "groupId": "1",
        "user": user_address,
        "contractName": contract_name,
        "version": "",
        "funcName": func_name,
        "funcParam": param,
        "contractAddress": contract_address,
        "contractAbi": abi,
        "useAes": False,
        "useCns": False,
        "cnsName": ""
    }
    headers = {'Content-Type': 'application/json'}
    res = requests.post(url="http://%s:5002/WeBASE-Front/trans/handle"%(config.webase_host),
                        headers=headers,
                        data=json.dumps(data).replace("False", "false").replace("True", "true"))
    return res
